package tree.reverse;

import tree.Tree;

import java.util.LinkedList;
import java.util.Queue;

/**
 * @author songZiHao
 * @version 1.0.0
 * @ClassName ReverseTree.java
 * @Description TODO
 * @createTime 2020年08月12日 09:46:00
 */
public class ReverseTree {
	public TreeNode reverse1(TreeNode root){
		if (root==null){
			return null;
		}
		TreeNode temp=root.left;
		root.left=root.right;
		root.right=temp;
		reverse1(root.left);
		reverse1(root.right);
		return root;
	}

	public TreeNode reverse2(TreeNode root){
		if (root==null){
			return null;
		}
		reverse2(root.left);
		reverse2(root.right);
		TreeNode temp=root.left;
		root.left=root.right;
		root.right=temp;
		return root;
	}

	public TreeNode reverse3(TreeNode root){
		if (root==null){
			return null;
		}
		reverse3(root.left);
		TreeNode temp=root.left;
		root.left=root.right;
		root.right=temp;
		reverse3(root.left);
		return root;
	}

	public TreeNode reverse4(TreeNode root){
		if (root==null){
			return null;
		}
		Queue<TreeNode> queue=new LinkedList<>();
		queue.offer(root);
		while (!queue.isEmpty()){
			TreeNode treeNode = queue.poll();
			TreeNode temp=treeNode.left;
			treeNode.left=treeNode.right;
			treeNode.right=temp;
			if (treeNode.left!=null){
				queue.offer(treeNode.left);
			}
			if (treeNode.right!=null){
				queue.offer(treeNode.right);
			}
		}
		return root;
	}
}
